package com.yang.core.service;

import com.yang.core.dao.AddressRepository;
import com.yang.core.dao.ProductImageRepository;
import com.yang.core.dao.ProductRepository;
import com.yang.core.model.Address;
import com.yang.core.model.Product;
import com.yang.core.model.ProductImage;
import com.yang.service.api.AddrService;
import com.yang.service.api.ProductService;
import com.yang.service.model.dto.post.AddrPostDTO;
import com.yang.service.model.dto.put.AddrPutDTO;
import com.yang.service.model.vo.AddrVo;
import com.yang.service.model.vo.ProductVO;
import com.zhy.base.exception.model.ExceptionEnum;
import com.zhy.base.exception.utils.AssertHelper;
import com.zhy.base.tools.beans.BeanMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.web.bind.annotation.*;

import javax.transaction.Transactional;
import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * Created by xin7k
 * Date: 2019/12/2
 */
@Slf4j
@RestController
@RequestMapping("product")
@Api(tags = "商品模块", description = "product")
public class ProductImpl implements ProductService {
    @Autowired
    private BeanMapper beanMapper;
    @Autowired
    private ProductRepository productRepository;
    @Autowired
    private ProductImageRepository productImageRepository;

    @ApiOperation("根据店铺id获取所有的商品")
    @GetMapping
    @Transactional
    @Override
    public List<ProductVO> selectAllProductByShopId(String shopId) {
        List<ProductVO> reslut = new ArrayList<>();
        //通过店铺id查询所有的商品
        Product product = new Product();
        product.setShopId(shopId);
        Example<Product> examp = Example.of(product);
        List<Product> all = productRepository.findAll(examp);
        if(!all.isEmpty()){
            //若找到了，就通过商品id去查找图片
            for(int i = 0; i < all.size(); i++){
                ProductVO map = beanMapper.map(all.get(i), ProductVO.class);
                ProductImage productImage = new ProductImage();
                productImage.setProId(all.get(i).getId());
                Example<ProductImage> example = Example.of(productImage);
                List<ProductImage> images = productImageRepository.findAll(example);
                if(!images.isEmpty()){
                    List<String> list = new ArrayList<>();
                    //若有图片
                    for(int j = 0; j < images.size(); j++){
                        list.add(images.get(j).getImgAddr());
                    }
                    map.setProductImgs(list);
                }else{
                    //没有设置为空
                    map.setProductImgs(null);
                }
                reslut.add(map);
            }
            return reslut;
        }else{
            return null;
        }

    }


    @ApiOperation("根据商品id获取细节")
    @GetMapping("details")
    @Transactional
    @Override
    public ProductVO getProductDetailsByProductId(String productId) {
        ProductVO result = new ProductVO();
        Product product = new Product();
        product.setId(productId);
        Example<Product> example = Example.of(product);
        Optional<Product> one = productRepository.findOne(example);
        if(one.isPresent()){
           result = beanMapper.map(one.get(),ProductVO.class);
            //若有商品,查找图片
            ProductImage productImage = new ProductImage();
            productImage.setProId(productId);
            Example<ProductImage> example1 = Example.of(productImage);
            List<ProductImage> images = productImageRepository.findAll(example1);
            if(!images.isEmpty()) {
                List<String> list = new ArrayList<>();
                //若有图片
                for (int j = 0; j < images.size(); j++) {
                    list.add(images.get(j).getImgAddr());
                }
                result.setProductImgs(list);
            }else{
                //没有图片
                result.setProductImgs(null);
            }
            return result;
        }else{
            return null;
        }
    }
}
